<template>
  <div class="app-container">
    <div class="container">
      <el-row :gutter="20" class="filter-container">
        <el-col :span="15">
          <el-button
            v-waves
            class="filter-item margin-left20 upload-demo"
            type="primary"
            size="mini"
            @click="outloadFile"
          >
            <i class="el-icon-download" />&nbsp;导出
          </el-button>
        </el-col>
        <el-col :span="9" style="text-align: right;" >
          <!-- <el-select
            v-model="listQuery.departId"
            style="width:130px;padding-right:12px"
            placeholder="请选择部门"
            size="mini"
            clearable
            @change="queryDepartC"
          >
            <el-option
              v-for="item in options10"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select> -->
          <el-input
            v-model="listQuery.search"
            style="width:150px;padding-right:12px"
            :placeholder="placeholderName"
            size="mini"
          />
          <el-button size="mini" type="primary" @click="getList">
            搜索
          </el-button>
        </el-col>
        <!-- <el-col :span="4" style="text-align: right;" :offset="16">
          <el-select
            v-model="listQuery.departId"
            placeholder="请选择部门"
            size="mini"
            clearable
            @change="queryDepartC"
          >
            <el-option
              v-for="item in options10"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="listQuery.search"
            placeholder="请输入用户名"
            size="mini"
          >
            <i slot="suffix" class="el-input__icon el-icon-search" @click="getList" />
          </el-input>
        </el-col> -->
      </el-row>

      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="用户名">
          <template slot-scope="scope">
            {{ scope.row.realName ? scope.row.realName : scope.row.userName }}
          </template>
        </el-table-column>
        <el-table-column label="操作说明" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.operation }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="请求方式" align="center" width="200">
          <template slot-scope="scope">
            {{ scope.row.method }}
          </template>
        </el-table-column> -->
        <!--        <el-table-column label="请求参数" align="center" width="200">-->
        <!--          <template slot-scope="scope">-->
        <!--            <span class="strinExceeds" :title="scope.row.param">{{ scope.row.param }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!-- <el-table-column label="后台路经" align="center" width="200">
          <template slot-scope="scope">
            {{ scope.row.controller }}
          </template>
        </el-table-column> -->
        <!--        <el-table-column label="请求耗时" align="center" width="200">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.timeLength }}毫秒-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="请求ip" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.ip }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作日期" width="" align="center">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <!--  <el-table-column label="请求ip" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.ip }}</span>
          </template>
        </el-table-column>
        <el-table-column label="请求ip" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.ip }}</span>
          </template>
        </el-table-column>
        <el-table-column label="请求ip" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.ip }}</span>
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="版本" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.author }}
          </template>
        </el-table-column>
        <el-table-column label="授权" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >已授权/未授权</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
  </div>
</template>

<script>
import {
  departmentList
} from '@/api/department'
import { logList, assetsExport } from '@/api/log'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      value1: '',
      value2: '',
      total: 10,
      list: null,
      listLoading: true,
      listQuery: {
        departId: '',
        page: 1,
        limit: 10,
        search: ''
      },
      options: [{
        value: '1',
        label: '全部软件'
      }, {
        value: '2',
        label: '办公软件'
      }],
      options10: [],
      value: '1',
      placeholderName: '请输入用户名'
    }
  },
  created() {
    this.fetchData()
    this.getDepartmentList()
  },
  methods: {
    outloadFile() {
      assetsExport(this.listQuery).then(res => {
        if (res.code === 200) {
          // this.getList()
          const link = document.createElement('a')
          link.style.display = 'none'
          link.setAttribute('download', '日志导出')
          link.href = res.message
          document.body.appendChild(link)
          link.click()
          // window.open(res.message)
        }
      })
    },
    getDepartmentList() {
      departmentList({
        limit: 50,
        page: 1
      }).then(res => {
        if (res.code === 200) {
          this.options10 = []
          res.data.data.map(item => {
            if (item.id !== '1025' && item.id !== '55904214559285248') {
              this.options10.push(item)
            }
          })
        }
      })
    },
    queryDepartC(val) {
      if (val) {
        this.placeholderName = '所有人'
      } else {
        this.placeholderName = '请输入用户名'
      }
    },
    handleEdit(index, row) {
    },
    handleDelete(index, row) {
    },
    getList() {
      this.listLoading = true
      logList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    fetchData() {
      this.getList()
    }
  }
}
</script>

<style scoped>
  .text-left {
    text-align: left;
  }
  .text-right {
    text-align: right;
    padding-right: 20px ;
  }
  .filter-container{
    margin: 20px;
  }
</style>
